{% extends "admin/base_site.html" %}

<!-- LOADING -->
{% load static i18n admin_modify admin_urls grp_tags %}

<!-- STYLESHEETS -->
{% block stylesheets %}
    {{ block.super }}
{% endblock %}

<!-- JAVASCRIPTS -->
{% block javascripts %}
    {{ block.super }}
    {% url 'admin:jsi18n' as jsi18nurl %}
    <script type="text/javascript" src="{{ jsi18nurl|default:'../../../jsi18n/' }}"></script>
    <script type="text/javascript" charset="utf-8">
        (function($) {
            $(document).ready(function() {
                grappelli.initDateAndTimePicker();
                $("#grp-content-container .grp-group").grp_collapsible_group();
                $("#grp-content-container .grp-collapse").grp_collapsible({
                    on_init: function(elem, options) {
                        // open collapse (and all collapse parents) in case of errors
                        if (elem.find("ul.errorlist").length > 0) {
                            elem.removeClass("grp-closed")
                                .addClass("grp-open");
                            elem.parents(".grp-collapse")
                                .removeClass("grp-closed")
                                .addClass("grp-open");
                        }
                    }
                });
                var related_lookup_fields_fk = {% get_related_lookup_fields_fk adminform.model_admin %};
                var related_lookup_fields_m2m = {% get_related_lookup_fields_m2m adminform.model_admin %};
                var related_lookup_fields_generic = {% get_related_lookup_fields_generic adminform.model_admin %};
                var autocomplete_fields_fk = {% get_autocomplete_lookup_fields_fk adminform.model_admin %};
                var autocomplete_fields_m2m = {% get_autocomplete_lookup_fields_m2m adminform.model_admin %};
                var autocomplete_fields_generic = {% get_autocomplete_lookup_fields_generic adminform.model_admin %};
                $.each(related_lookup_fields_fk, function() {
                    $("#id_" + this).grp_related_fk({lookup_url:"{% url 'grp_related_lookup' %}"});
                });
                $.each(related_lookup_fields_m2m, function() {
                    $("#id_" + this).grp_related_m2m({lookup_url:"{% url 'grp_m2m_lookup' %}"});
                });
                $.each(related_lookup_fields_generic, function() {
                    var content_type = "#id_" + this[0],
                        object_id = "#id_" + this[1];
                    $(object_id).grp_related_generic({content_type:content_type, object_id:object_id, lookup_url:"{% url 'grp_related_lookup' %}"});
                });
                $.each(autocomplete_fields_fk, function() {
                    $("#id_" + this).grp_autocomplete_fk({
                        lookup_url:"{% url 'grp_related_lookup' %}",
                        autocomplete_lookup_url:"{% url 'grp_autocomplete_lookup' %}"
                    });
                });
                $.each(autocomplete_fields_m2m, function() {
                    $("#id_" + this).grp_autocomplete_m2m({
                        lookup_url:"{% url 'grp_m2m_lookup' %}",
                        autocomplete_lookup_url:"{% url 'grp_autocomplete_lookup' %}"
                    });
                });
                $.each(autocomplete_fields_generic, function() {
                    var content_type = "#id_" + this[0],
                        object_id = "#id_" + this[1];
                    $(object_id).grp_autocomplete_generic({
                        content_type:content_type,
                        object_id:object_id,
                        lookup_url:"{% url 'grp_related_lookup' %}",
                        autocomplete_lookup_url:"{% url 'grp_autocomplete_lookup' %}"
                    });
                });
                $("a#grp-open-all").on("click", function(){
                    $("#grp-content .grp-collapse-handler").each(function() {
                        $(this).parent(".grp-collapse").removeClass("grp-closed").addClass("grp-open");
                    });
                });
                $("a#grp-close-all").on("click", function(){
                    $("#grp-content .grp-collapse-handler").each(function() {
                        $(this).parent(".grp-collapse").removeClass("grp-open").addClass("grp-closed");
                    });
                });
                // HACK: get rid of currently/change with URL–fields. F**K!!!
                {% block js_remove_url %}
                $('p.url').each(function() {
                    $(this).find("a").remove();
                    var text = $(this).html();
                    text = text.replace(/^\w*: /, "");
                    text = text.replace(/<br>.*: /, "");
                    $(this).html(text);
                });
                {% endblock %}
                // HACK: rearrange inlines
                {% block js_rearrange_inlines %}
                $('div.grp-group').each(function() {
                    var placeholder = $("fieldset.placeholder."+$(this).attr("id"));
                    if (placeholder.length) {
                        $(placeholder).replaceWith($(this));
                    }
                });
                {% endblock %}
                // HACK: remove input types
                {% block js_clean_input_types %}
                var clean_input_types = "{% grappelli_clean_input_types %}";
                if (clean_input_types == "True") {
                    grappelli.cleanInputTypes();
                };
                {% endblock %}
            });
        })(grp.jQuery);
    </script>
    {{ media }}
{% endblock %}

{% block extrahead %}
    {{ block.super }}
    <script type="text/javascript">
        (function($) {
            $(document).ready(function() {
                $('.add-another').on("click", function(e) {
                    e.preventDefault();
                    showAddAnotherPopup(this);
                });
                $('.related-lookup').on("click", function(e) {
                    e.preventDefault();
                    showRelatedObjectLookupPopup(this);
                });
            {% if adminform and add %}
                $('form#{{ opts.model_name }}_form :input:visible:enabled:first').focus()
            {% endif %}
            });
        })(grp.jQuery);
    </script>
{% endblock %}

<!-- COLTYPE/BODYCLASS -->
{% block bodyclass %}{{ opts.app_label }}-{{ opts.object_name.lower }} grp-change-form{% endblock %}
{% block content-class %}{% endblock %}

<!-- PAGE-TOOLS -->
{% block page-tools %}
    {% if not is_popup %}
        <ul>
            <li><a href="javascript://" class="grp-tool" id="grp-open-all" title="{% trans 'Open All Items' %}">&nbsp;</a></li>
            <li><a href="javascript://" class="grp-tool" id="grp-close-all" title="{% trans 'Close All Items' %}">&nbsp;</a></li>
        </ul>
    {% endif %}
{% endblock %}

<!-- BREADCRUMBS -->
{% block breadcrumbs %}
    {% if not is_popup %}
        <ul>
            <li><a href="{% url 'admin:index' %}">{% trans "Home" %}</a></li>
            <li><a href="{% url 'admin:app_list' app_label=opts.app_label %}">{{ opts.app_config.verbose_name }}</a></li>
            <li>{% if has_change_permission %}
                {% url opts|admin_urlname:'changelist' as changelist_url %}
                <a href="{% add_preserved_filters changelist_url %}">{{ opts.verbose_name_plural|capfirst }}</a>{% else %}{{ opts.verbose_name_plural|capfirst }}
            {% endif %}</li>
            <li>{% if add %}{% trans "Add" %} {{ opts.verbose_name }}{% else %}{{ original|truncatewords:"18" }}{% endif %}</li>
        </ul>
    {% endif %}
{% endblock %}

<!-- OBJECT TOOLS -->
{% block object-tools %}
    {% if change %}
        {% if not is_popup %}
            <ul class="grp-object-tools">
                {% block object-tools-items %}
                    {% url opts|admin_urlname:'history' original.pk|admin_urlquote as history_url %}
                    <li><a href="{% add_preserved_filters history_url %}">{% trans "History" %}</a></li>
                    {% if has_absolute_url %}<li><a href="{{ absolute_url }}" class="grp-state-focus" target="_blank">{% trans "View on site" %}</a></li>{% endif%}
                {% endblock %}
            </ul>
        {% endif %}
    {% endif %}
{% endblock %}

<!-- CONTENT -->
{% block content %}
    <form {% if has_file_field %}enctype="multipart/form-data" {% endif %}action="{{ form_url }}" method="post" id="{{ opts.model_name }}_form" novalidate>{% csrf_token %}{% block form_top %}{% endblock %}
        <div>
            <!-- Popup Hidden Field -->
            {% if is_popup %}<input type="hidden" name="_popup" value="1" />{% endif %}

            <!-- No Submit-Row on Top -->

            <!-- Errors -->
            {% if errors %}
                <p class="errornote">{% if errors|length == 1 %}{% trans "Please correct the error below." %}{% else %}{% trans "Please correct the errors below." %}{% endif %}</p>
                <ul class="errorlist">{% for error in adminform.form.non_field_errors %}<li>{{ error }}</li>{% endfor %}</ul>
            {% endif %}

            <!-- Fieldsets -->
            {% block field_sets %}
                {% for fieldset in adminform %}
                    {% include "admin/includes/fieldset.html" %}
                {% endfor %}
            {% endblock %}

            {% block after_field_sets %}{% endblock %}

            <!-- Inlines -->
            {% block inline_field_sets %}
                {% for inline_admin_formset in inline_admin_formsets %}
                    {% include inline_admin_formset.opts.template %}
                {% endfor %}
            {% endblock %}

            {% block after_related_objects %}{% endblock %}

            <!-- Submit-Row -->
            {% block submit_buttons_bottom %}{% submit_row %}{% endblock %}

            <!-- JS for prepopulated fields -->
            {% prepopulated_fields_js %}

        </div>
    </form>
{% endblock %}
